<?php
/**
 * User: @lose
 * Date: 2020/7/28
 * Time: 2:02 下午
 */

namespace Beerus\Modules\BlackCard\Controllers;

use App\Admin\Extensions\Tools\UploadButton;
use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Grid;
use Illuminate\Http\Request;
use Biz\model\wiki\Wiki as mWiki;
use Doraemon\service\BlackCard\AdminWiki as sAdminWiki;
use Encore\Admin\Widgets\Tab;


class WikiController extends Controller
{
    public function index(Content $content, Request $request)
    {
        $tab = new Tab();
        $tab->addLink('节选商品', '/admin/blackCard');
        $tab->addLink('好好住商品', '/admin/blackCard/goods/index');

        $where = $this->buildWhere($request->all());
        $page = $request->input('page', 1);
        $perPage = $request->input('per_page', 20);

        $limit = [
            ($page - 1) * $perPage, $perPage
        ];

        $result = sAdminWiki::getList($where, $limit);
        $grid = new pGridDataModel($result['list'], $result['total'], [
            'perPage' => $perPage
        ]);

        $gridList = Admin::grid($grid,
            function (Grid $grid) {
                $grid->tools(function (Grid\Tools $tool) {
                    $tool->append(new UploadButton('uploadWiki', '导入黑卡商品'));
                });

                $grid->filter(function($filter){
                    // 去掉默认的id过滤器
                    $filter->disableIdFilter();
                    // 添加字段过滤器
                    $filter->like('channel_spu_id', '有赞商品id');
                    $filter->like('title', 'wiki名称');
                });

                $grid->disableExport();
                $grid->expandFilter();
                $grid->disableRowSelector();
                $grid->disableCreateButton();

                $grid->column("id", 'wiki_id');
                $grid->column("channel_spu_id", '有赞商品id');
                $grid->column("title", '标题')->width(300);
                $grid->column("cover_img", '商品头图')->image($this->cover_img, 100, 100);
                $grid->column("min_price", '最低价');
                $grid->column("max_price", '最高价');
                $grid->column("status", '状态')->display(function () {
                    return mWiki::ADM_STATUS_MAP[$this->status];
                });
                $grid->column("is_black", "是否为黑卡商品")->switchToggle();
                $grid->column("rebate_ratio", '返佣比例')->editable()->help('举例： 若返佣10% 填写10即可');
                $grid->disableActions();
            });

        return $content
            ->header('黑卡小程序wiki')
            ->body($tab)
            ->row($gridList)
            ->description(' ')
            ->breadcrumb(['text' => 'wiki']);
    }

    public function setBlack($wikiId, Request $request)
    {
        $message = '修改成功';
        try {
            $isBlack = $request->input('is_black', null);
            $name    = $request->input('name', null);
            $value   = $request->input('value', null);

            if(!is_null($isBlack)) {
                $isBlack = $isBlack == 'on' ? 1 : 0;
                $result = mWiki::updateData(['is_black' => $isBlack], ['id' => $wikiId]);
            }

            if($name == 'rebate_ratio') {
                $result = mWiki::updateData(['rebate_ratio' => $value], ['id' => $wikiId]);
            }

            if(!$result) {
                throw new \Exception('修改失败');
            }
            $status = true;
        } catch (\Exception $e) {
            $message = $e->getMessage();
            $status = false;
        }

        $response = compact('status', 'message');
        return response()->json($response);
    }

    public function uploadWiki()
    {
        $file = $_FILES['file'];
        sAdminWiki::uploadWiki($file);
    }

    private function buildWhere($params)
    {
        $where = [];
        if(isset($params['channel_spu_id']) && $params['channel_spu_id']) {
            $where['channel_spu_id'] = $params['channel_spu_id'];
        }

        if(isset($params['title']) && $params['title']) {
            $where['title[~]'] = $params['title'];
        }

        $where['channel'] = mWiki::CHANNEL_YOUZAN;

        return $where;
    }
}